﻿using BaoShouTangServer.DTOModel;
using BaoShouTangServer.Models;
using BaoShouTangServer.Service;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace BaoShouTangServer.Controllers
{
    public class SendMessageController : Controller
    {
        private static readonly NLog.Logger Logger = NLog.LogManager.GetCurrentClassLogger();
        // GET: SendMessage
        public ActionResult Index()
        {
            return View();
        }

        private ActionResult sendToEmail()
        {
            return View();
        }

        //发送派工信息
        [HttpPost]
        public JsonResult sendPaiGongToWechat(List<long> Z派工idList)
        {

            string msg = "";
            foreach (var z派工id in Z派工idList)
            {
                Logger.Warn("强制派工任务，id：" +z派工id );
                Util.SendWeChatWithImage(z派工id, out msg);
                //Util.SendMailByGmail("","","","","","",z派工id);
            }

            HttpData<object> httpData = new HttpData<object>();
            httpData.code = 1;
            httpData.msg = msg;
            return Json(httpData);
        }

        //发送报工不良信息
        [HttpPost]
        public JsonResult sendQCToWechat(List<Guid> Z派工idList)
        {

            string msg = "";
            foreach (var z派工id in Z派工idList)
            {
                Logger.Warn("报工不良，id：" + z派工id);
                Util.SendWeChatWithImageQC(z派工id, out msg);
                //Util.SendMailByGmail("","","","","","",z派工id);
            }

            HttpData<object> httpData = new HttpData<object>();
            httpData.code = 1;
            httpData.msg = msg;
            return Json(httpData);
        }

        //定时发送派工信息
        [HttpPost]
        public JsonResult sendPaiGongToWechatInTimer(Z_派工 Z派工)
        {
            Logger.Warn("定时派工任务，id：" + Z派工.id);
            string msg = "";
            Util.SendWeChatWithImage(Z派工.id, out msg);
            //Util.SendMailByGmail("","","","","","",z派工id);

            HttpData<object> httpData = new HttpData<object>();
            httpData.code = 1;
            httpData.msg = msg;
            return Json(httpData);
        }

        //发送取消派工信息
        [HttpPost]
        public JsonResult cancelPaiGongToWechat(List<long> id)
        {
            Logger.Warn("取消派工收到，id：" + id);
            string msg = "";
            HttpData<object> httpData = new HttpData<object>();
            List<Z_派工> z_s = SqlSugarHelp.scope.Queryable<Z_派工>().Where(it => id.Contains(it.id)).IgnoreColumns(it => it.工票二维码).ToList();
            foreach (var item in z_s)
            {
                if (item.发送日期 != null)
                {
                    Logger.Warn("取消派工，已发送工票的需要通知：" + item.id);
                    string sendmesg = "[" + item.cUser_Name + "]" + "您的工票已取消。订单号[" + item.订单号 + "]。订单行号[" + item.订单行号 + "]。产品编号[" + item.产品编号 + "]。产品名称[" + item.产品名称 + "]。厂别[" + item.厂别 + "]。工序名称[" + item.工序名称 + "]。开工日期[" + item.开工日期.GetValueOrDefault().ToString("yyy-MM-dd") + "]。计划完工日期[" + item.计划完工日期.GetValueOrDefault().ToString("yyy-MM-dd") + "]。派工数量[" + item.派工数量.GetValueOrDefault() + "]。";
                    Util.SendWeChatWithText(item.微信号, sendmesg, out msg);
                    Logger.Warn("取消派工，已发送工票的需要通知发送结果：" + item.id+"--"+msg);
                    if (msg == "ok")
                    {
                        int r = SqlSugarHelp.scope.Deleteable<Z_派工>(z_s).ExecuteCommand();
                        if (r > 0)
                        {
                            httpData.msg = "已成功取消派工";
                        }
                        else
                        {
                            httpData.msg = "发送取消工票信息成功，删除派工失败";
                        }
                    }
                }
                else
                {
                    int r = SqlSugarHelp.scope.Deleteable<Z_派工>(z_s).ExecuteCommand();
                    if (r > 0)
                    {
                        httpData.msg = "已成功取消派工";
                    }
                    else
                    {
                        httpData.msg = "发送取消工票信息成功，删除派工失败";
                    }
                }
            }
            httpData.code = 1;
            return Json(httpData);
        }

        //发送备料信息
        //[HttpGet]
        //public JsonResult sendBeiLiaoToWechat(string paiGongId,string beiLiaoName)
        //{
        //    Logger.Warn("备料接口，收到的id" + paiGongId);
        //    //非倒冲仓的材料进行材料出库
        //    HttpData<List<DTOU8材料出库单导入结果>> httpData = new HttpData<List<DTOU8材料出库单导入结果>>();
        //    long.TryParse(paiGongId, out long id);
        //    string msg = "";
        //    Z_派工 z_派工 = SqlSugarHelp.scope.Queryable<Z_派工>().Where(it => it.id == id).First();
        //    Logger.Warn("备料接口，查询到的派工"+z_派工.id);
        //    if (z_派工 != null)
        //    {
        //        List<DTOU8材料出库单导入结果> result = U8Service.生成U8材料出库单(id, beiLiaoName);
                
        //        //有一个导入成功就更改发料状态，并发送微信信息
        //        foreach (var item in result)
        //        {
        //            if (item.导入结果 == "ok")
        //            {
        //                //发送备料微信消息
        //                string sendmesg = "[" + z_派工.cUser_Name + "]" + "您的工票已备料。订单号[" + z_派工.订单号 + "]。订单行号[" + z_派工.订单行号 + "]。产品编号[" + z_派工.产品编号 + "]。产品名称[" + z_派工.产品名称 + "]。厂别[" + z_派工.厂别 + "]。工序名称[" + z_派工.工序名称 + "]。开工日期[" + z_派工.开工日期.GetValueOrDefault().ToString("yyy-MM-dd") + "]。计划完工日期[" + z_派工.计划完工日期.GetValueOrDefault().ToString("yyy-MM-dd") + "]。派工数量[" + z_派工.派工数量 + "]。";
        //                Util.SendWeChatWithText(z_派工.微信号, sendmesg, out msg);
        //                int r = SqlSugarHelp.scope.Updateable<Z_派工>().SetColumns(it => it.状态 == "已发料").SetColumns(it =>it.发料人 == beiLiaoName).Where(it => it.id == z_派工.id).ExecuteCommand();
        //                if (r > 0)
        //                {
        //                    httpData.msg = "更新发料状态成功";
        //                }
        //                else
        //                {
        //                    httpData.msg = "更新发料状态失败";
        //                }
        //                break;
        //            }
        //        }
        //        httpData.code = 1;
        //        httpData.data = result;
        //    }
        //    else
        //    {
        //        httpData.code = 0;
        //        httpData.msg = "没有该工票信息";

        //    }
           
         
        //    return Json(httpData, JsonRequestBehavior.AllowGet);
        //}

        [HttpPost]
        public JsonResult sendBeiLiaoToWechat(DTOSendBeiLiao dtoSendBeiLiao)
        {
            Logger.Warn("备料接口，收到的id" + dtoSendBeiLiao.paigongId);
            long id = dtoSendBeiLiao.paigongId;
            string beiLiaoName = dtoSendBeiLiao.beiLiaoRen.cUser_Name;
            string wechatCode ="";
            foreach (var item in dtoSendBeiLiao.kugong)
            {
                wechatCode = wechatCode + "|" + item.cUserEmail;
            }
            //非倒冲仓的材料进行材料出库
            HttpData<List<DTOU8材料出库单导入结果>> httpData = new HttpData<List<DTOU8材料出库单导入结果>>();
            //long.TryParse(paiGongId, out long id);
            string msg = "";
            Z_派工 z_派工 = SqlSugarHelp.scope.Queryable<Z_派工>().Where(it => it.id == id).IgnoreColumns(it => it.工票二维码).First();
            Logger.Warn("备料接口，查询到的派工" + z_派工.ToString());
            if (z_派工 != null)
            {
                List<DTOU8材料出库单导入结果> result = U8Service.生成U8材料出库单(id, beiLiaoName);
               
                //有一个导入成功就更改发料状态，并发送微信信息
                foreach (var item in result)
                {
                   
                    if (item.导入结果 == "ok")
                    {
                        //发送备料微信消息
                        
                        string sendmesg = "[" + z_派工.cUser_Name + "]" + "您的工票已备料。订单号[" + z_派工.订单号 + "]。订单行号[" + z_派工.订单行号 + "]。产品编号[" + z_派工.产品编号 + "]。产品名称[" + z_派工.产品名称 + "]。厂别[" + z_派工.厂别 + "]。工序名称[" + z_派工.工序名称 + "]。开工日期[" + z_派工.开工日期.GetValueOrDefault().ToString("yyy-MM-dd") + "]。计划完工日期[" + z_派工.计划完工日期.GetValueOrDefault().ToString("yyy-MM-dd") + "]。派工数量[" + z_派工.派工数量 + "]。";
                        Util.SendWeChatWithText(z_派工.微信号+ wechatCode, sendmesg, out msg);
                        int r = SqlSugarHelp.scope.Updateable<Z_派工>()
                            //.SetColumns(it => it.状态 == "已发料")
                            .SetColumns(it => it.发料人 == beiLiaoName)
                            .SetColumns(it => it.发料状态 == "已发料")
                            .Where(it => it.id == z_派工.id).ExecuteCommand();
                        if (r > 0)
                        {
                            httpData.msg = "更新发料状态成功";
                        }
                        else
                        {
                            httpData.msg = "更新发料状态失败";
                        }
                        break;
                    }
                }
                httpData.code = 1;
                httpData.data = result;
                Logger.Warn("备料接口，查询到的派工" + z_派工.ToString()+"\r\n"+"材料出库单导入结果："+ JsonExtends.ToJson(result));
            }
            else
            {
                httpData.code = 0;
                httpData.msg = "没有该工票信息";

            }


            return Json(httpData, JsonRequestBehavior.AllowGet);
        }


        [HttpPost]
        public JsonResult sendBeiLiaoWitchBatchToWechat(DTOSendBeiLiao dtoSendBeiLiao)
        {
            Logger.Warn("备料接口，收到的id" + dtoSendBeiLiao.paigongId);
            long id = dtoSendBeiLiao.paigongId;
            string beiLiaoName = dtoSendBeiLiao.beiLiaoRen.cUser_Name;
            string wechatCode = "";
            foreach (var item in dtoSendBeiLiao.kugong)
            {
                wechatCode = wechatCode + "|" + item.cUserEmail;
            }
            //非倒冲仓的材料进行材料出库
            HttpData<List<DTOU8材料出库单导入结果>> httpData = new HttpData<List<DTOU8材料出库单导入结果>>();
            //long.TryParse(paiGongId, out long id);
            string msg = "";
            Z_派工 z_派工 = SqlSugarHelp.scope.Queryable<Z_派工>().Where(it => it.id == id).IgnoreColumns(it => it.工票二维码).First();
            Logger.Warn("备料接口，查询到的派工" + z_派工.ToString());
            if (z_派工 != null)
            {
                List<DTOU8材料出库单导入结果> result = U8Service.按批号生成U8材料出库单(id, beiLiaoName, dtoSendBeiLiao.dTOBOMDetails实际用量);

                //有一个导入成功就更改发料状态，并发送微信信息
                foreach (var item in result)
                {

                    if (item.导入结果 == "ok")
                    {
                        //发送备料微信消息

                        string sendmesg = "[" + z_派工.cUser_Name + "]" + "您的工票已备料。订单号[" + z_派工.订单号 + "]。订单行号[" + z_派工.订单行号 + "]。产品编号[" + z_派工.产品编号 + "]。产品名称[" + z_派工.产品名称 + "]。厂别[" + z_派工.厂别 + "]。工序名称[" + z_派工.工序名称 + "]。开工日期[" + z_派工.开工日期.GetValueOrDefault().ToString("yyy-MM-dd") + "]。计划完工日期[" + z_派工.计划完工日期.GetValueOrDefault().ToString("yyy-MM-dd") + "]。派工数量[" + z_派工.派工数量 + "]。";
                        Util.SendWeChatWithText(z_派工.微信号 + wechatCode, sendmesg, out msg);
                        int r = SqlSugarHelp.scope.Updateable<Z_派工>()
                            //.SetColumns(it => it.状态 == "已发料")
                            .SetColumns(it => it.发料人 == beiLiaoName)
                            .SetColumns(it => it.发料状态 == "已发料")
                            .Where(it => it.id == z_派工.id).ExecuteCommand();
                        if (r > 0)
                        {
                            httpData.msg = "更新发料状态成功";
                        }
                        else
                        {
                            httpData.msg = "更新发料状态失败";
                        }
                        break;
                    }
                }
                httpData.code = 1;
                httpData.data = result;
                Logger.Warn("备料接口，查询到的派工" + z_派工.ToString() + "\r\n" + "材料出库单导入结果：" + JsonExtends.ToJson(result));
            }
            else
            {
                httpData.code = 0;
                httpData.msg = "没有该工票信息";

            }


            return Json(httpData, JsonRequestBehavior.AllowGet);
        }

        [HttpPost]
        public JsonResult zhuanPai(string oldUserId, string zhuanPaiUserId,string paiGongId)
        {
            Logger.Warn("给旧派工人员发送取消派工的消息oldUserId：" + oldUserId);
            Logger.Warn("给旧派工人员发送取消派工的消息zhuanPaiUserId：" + zhuanPaiUserId);
            Logger.Warn("给旧派工人员发送取消派工的消息paiGongId：" + paiGongId);
            HttpData<object> httpData = new HttpData<object>();
            UA_User oldUser = SqlSugarHelp.scope.Queryable<UA_User>().Where(it => it.cUser_Id == oldUserId).First();
            UA_User newUser = SqlSugarHelp.scope.Queryable<UA_User>().Where(it => it.cUser_Id == zhuanPaiUserId).First();
            Z_派工 z_派工 = SqlSugarHelp.scope.Queryable<Z_派工>().Where(it => it.id == long.Parse(paiGongId)).First();
            string msg = "";
            //给新人员发送派工信息
            if (oldUser != null && z_派工 != null)
            {
                Logger.Warn("给旧派工人员发送取消派工的消息");
                string sendmesg = "[" + oldUser.cUser_Name + "]" + "您的工票已取消。订单号[" + z_派工.订单号 + "]。订单行号[" + z_派工.订单行号 + "]。产品编号[" + z_派工.产品编号 + "]。产品名称[" + z_派工.产品名称 + "]。厂别[" + z_派工.厂别 + "]。工序名称[" + z_派工.工序名称 + "]。开工日期[" + z_派工.开工日期.GetValueOrDefault().ToString("yyy-MM-dd") + "]。计划完工日期[" + z_派工.计划完工日期.GetValueOrDefault().ToString("yyy-MM-dd") + "]。派工数量[" + z_派工.派工数量.GetValueOrDefault() + "]。";
                Util.SendWeChatWithText(oldUser.cUserEmail, sendmesg, out msg);
            }
            //给旧派工人员发送取消派工的消息

            if (newUser != null && z_派工 != null)
            {
                Logger.Warn("给新派工人员发送取消派工的消息");
                Util.SendWeChatWithImage(long.Parse(paiGongId), out msg);
            }
            httpData.msg = "转派成功";
            return Json(httpData);
        }
    }
}